#!/sbin/ash
# shellcheck shell=dash

# Copyright 2017 Attila Szöllősi
#
# This file is part of postmarketos-android-recovery-installer.
#
# postmarketos-android-recovery-installer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# postmarketos-android-recovery-installer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with postmarketos-android-recovery-installer.  If not, see <http://www.gnu.org/licenses/>.

# shellcheck source=pmos_install_functions
. /tmp/postmarketos/bin/pmos_install_functions "$1" "$2"
# shellcheck source=/dev/null
. "$WORKING_DIR"/install_options

exec > "$WORKING_DIR"/pmos.log 2>&1
set -ex

ui_print "                                         "
ui_print "                    8                    "
ui_print "                   888                   "
ui_print "                  88888                  "
ui_print "                 8888888                 "
ui_print "                888888888                "
ui_print "               88888888888               "
ui_print "               888888888888              "
ui_print "             8  888888888888             "
ui_print "            888      88888888            "
ui_print "           88888888   88888888           "
ui_print "          88888888     88888888          "
ui_print "         88888888       88888888         "
ui_print "        88888888         88888888        "
ui_print "       88888888           888            "
ui_print "      88888888             8  88888      "
ui_print "     88888888                8888888     "
ui_print "    88888888                 88888888    "
ui_print "   88888888888  8888888888888888888888   "
ui_print "  8888888888888  8888888888888888888888  "
ui_print " 8888888888888  888888888888888888888888 "
ui_print "8888888888888  88888888888888888888888888"
ui_print "                                         "
ui_print "postmarketOS recovery installer          "
ui_print "                                         "

ui_print "Entering working directory..."
cd "$WORKING_DIR"
ui_print "Extracting files..."
busybox unzip -o "$ZIP" -x rootfs.tar.gz
mkdir -p /lib
ui_print "Symlinking .so files to /lib/..."
ln -sf "$WORKING_DIR"/lib/* /lib/
ui_print "Symlinking block devices..."
ln -sf /dev/block/* /dev/
ui_print "Extracting partition table..."
extract_partition_table
ui_print "Unmounting /$INSTALL_PARTITION..."
umount_install_partition
ui_print "Creating partition table on $INSTALL_DEVICE..."
# parted returns nonzero even when command executed successfully
partition_install_device || :
if [ "$FDE" = "true" ]
then
	[ -e /dev/mapper/pm_crypt ] && cryptsetup close pm_crypt
	ui_print "Generating temporary keyfile with random data..."
	busybox dd bs=512 count=4 if=/dev/urandom of="$WORKING_DIR"/lukskey
	ui_print "Initializing LUKS device..."
	cryptsetup luksFormat --use-urandom -c "$CIPHER" -q "$ROOT_PARTITION" "$WORKING_DIR"/lukskey
	ui_print "Opening LUKS partition..."
	cryptsetup luksOpen -d "$WORKING_DIR"/lukskey "$ROOT_PARTITION" pm_crypt
	ui_print "Formatting LUKS partition..."
	mkfs.ext4 -L 'pmOS_root' /dev/mapper/pm_crypt
	ui_print "Mounting LUKS partition..."
	mount -t ext4 -rw /dev/mapper/pm_crypt /"$INSTALL_PARTITION"
else
	ui_print "Formatting root partition..."
	mkfs.ext4 -L 'pmOS_root' "$ROOT_PARTITION"
	ui_print "Mounting root partition..."
	mount -t ext4 -rw "$ROOT_PARTITION" /"$INSTALL_PARTITION"
fi
ui_print "Formatting pmOS_boot..."
mkfs.ext2 -q -L 'pmOS_boot' "$PMOS_BOOT"
ui_print "Mounting pmOS_boot..."
mkdir /"$INSTALL_PARTITION"/boot
mount -t ext2 -rw "$PMOS_BOOT" /"$INSTALL_PARTITION"/boot || {
	ui_print "Failed to format/mount ext2 partition."
	ui_print "Trying ext4..."
	mkfs.ext4 -L 'pmOS_boot' "$PMOS_BOOT"
	mount -t ext4 -rw "$PMOS_BOOT" /"$INSTALL_PARTITION"/boot
}
ui_print "Installing rootfs..."
busybox unzip -p "$ZIP" rootfs.tar.gz | busybox tar -xz -C /"$INSTALL_PARTITION"
if [ "$FLASH_KERNEL" = "true" ]
then
	if [ "$ISOREC" = "true" ]
	then
		ui_print "Flashing kernel..."
		busybox dd if=/"$INSTALL_PARTITION"/boot/vmlinuz-"$FLAVOR" of="$KERNEL_PARTITION"
		ui_print "Flashing initfs..."
		busybox gunzip -c /"$INSTALL_PARTITION"/boot/initramfs-"$FLAVOR" | busybox lzop \
			> "$INITFS_PARTITION"
	else
		ui_print "Flashing boot.img..."
		busybox dd if=/"$INSTALL_PARTITION"/boot/boot.img-"$FLAVOR" of="$BOOT_PARTITION"
	fi
fi
if [ "$FDE" = "true" ]
then
	ui_print "Creating a symlink for password setting script in /sbin/..."
	ln -sf "$WORKING_DIR"/bin/pmos_setpw /sbin/
	ui_print "Do not forget to add a password to the LUKS partition!"
	ui_print "Run the command: pmos_setpw from the terminal/adb shell!"
fi
ui_print "Installation done."
